package edu.gatech.cs2340.aperturescience;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import edu.gatech.cs2340.aperturescience.backend.category.Category;
import edu.gatech.cs2340.aperturescience.backend.category.CategoryDataSource;
import edu.gatech.cs2340.aperturescience.backend.category.SQLCategoryDataSource;
import edu.gatech.cs2340.aperturescience.backend.task.SQLTasksDataSource;
import edu.gatech.cs2340.aperturescience.backend.task.Task;
import edu.gatech.cs2340.aperturescience.backend.task.TasksDataSource;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TimePicker;

/**
 * CategoryViewActivity manages the Categories pane within the application
 * 
 * @author Aperture Science
 * @3/19/12
 */
public class CategoryViewActivity extends Activity {
	private ListView listView;

	private CategoryDataSource categorySource;
	private int userid;
	private CategoryListAdapter listAdapter;

	/**
	 * onDestroy closes the database
	 */
	protected void onDestroy() {
		super.onDestroy();
		if (categorySource != null) {
			categorySource.close();
		}
	}

	/**
	 * onCreate initiates the database and creates the default Categories
	 */
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		userid = getIntent().getIntExtra(
				"edu.gatech.cs2340.aperturescience.uid", 0);
		categorySource = new SQLCategoryDataSource(this);
		categorySource.open();
		
		if(categorySource.isEmpty(userid))
		{
			categorySource.updateCategory(userid, "All");
			categorySource.updateCategory(userid, "Today");
			categorySource.updateCategory(userid, "Completed");
		} 

		setContentView(R.layout.categorylistview);

		listView = (ListView) findViewById(R.id.categoryList);

		loadCategories();

		listView.setTextFilterEnabled(true);

		Button addCategoryButton = (Button) findViewById(R.id.addCategoryButton);
		final EditText categoryBox = (EditText) findViewById(R.id.newCategoryName);
		//Button showAllTasksButton = (Button) findViewById(R.id.showAllTasksButton);
		addCategoryButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String category = categoryBox.getText().toString();

				if (category != null) 
				{
					if(!categorySource.getAllCategories(userid).contains(category) && category.length() != 0)
					{
						categorySource.updateCategory(userid, category);
						categoryBox.getText().clear();
						loadCategories();
					}

					categoryBox.setText("");					
				}
				else
				{

				}
			}
		});

		/*showAllTasksButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {

				Intent i = new Intent(CategoryViewActivity.this,
						TaskViewActivity.class);
				i.putExtra("edu.gatech.cs2340.aperturescience.uid", userid);
				i.putExtra("category", "null");
				startActivity(i);

			}
		});*/

	}
	//for testing
	public int getCategoryCount()
	{
		return listAdapter.getCount();
	}

	/**
	 * loadCategories displays all existing Categories
	 */
	private void loadCategories() {
		List<Category> result = categorySource.getAllCategories(userid);
		Category[] categories = new Category[result.size()];
			result.toArray(categories);
		listAdapter = new CategoryListAdapter(this,
				R.layout.category_list_item, categories);
		listView.setAdapter(listAdapter);
		listView.setClickable(true);
		listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String category = (listAdapter.getItem(position).category);
				
				Intent i = new Intent(CategoryViewActivity.this, TaskViewActivity.class);
				
				i.putExtra("edu.gatech.cs2340.aperturescience.uid", userid);
				i.putExtra("category", category);
					
				startActivity(i);
			}

		});
	}

}